<template>
	<view class="container">
		<view class="bg-colore"></view>
		<view class="content">
			<uni-swipe-action>
				<uni-swipe-action-item>
					<view class="cart-list" v-for="(item,index) in goodList" :key="index">
						<view class="left">
							<image :src="item.active?'/static/sj_duihov.png':'/static/sj_dui.png'" mode="scaleToFill" class="gou"
								@click="dianjiActive(index)">
							</image>
						</view>
						<view class="right">
							<view class="img">
								<image :src="$public.imgageUrl(item.image)" mode="scaleToFill" class="image"></image>
							</view>
							<view class="text-div">
								<view class="title">
									{{item.title}}
								</view>
								<view class="text-bottom">
									<view class="money">
										<text>￥</text>{{item.money}}
									</view>
									<view class="">
										<image src="../../static/sj_delete.png" mode="scaleToFill" class="delete"
											@click="deleteCart(index)"></image>
									</view>
									<view class="num">
										<uni-number-box v-model="item.num" :max="item.maxNum?item.maxNum:500" background="#DF686A"
											color="#fff" class="numcss" @change="calcTotal" />
									</view>
								</view>
							</view>
						</view>
					</view>
					<!-- <template v-slot:right>
						<view class="delete-div">
							<image src="../../static/sj_delete.png" mode="scaleToFill" class="delete"></image>
						</view>
					</template> -->
				</uni-swipe-action-item>
			</uni-swipe-action>
			<view class="page-bottom">
				<view class="page-bottom-l">
					<image :src="allActive?'/static/sj_duihov.png':'/static/sj_dui.png'" mode="scaleToFill" class="gou"
						@click="dianJiQuanXuan"></image>
					全选

					<text class="zong-ji">总计:</text>
					<text class="zong-money">￥{{moneyTotal}}</text>
				</view>
				<view class="action-btn-group">
					<button type="primary" class=" btncss" @click="buttonClick()">去结算</button>
				</view>
			</view>
		</view>
	</view>

</template>

<script>
	export default {
		data() {
			return {
				goodList: [],
				typeId: 6,
				allActive: true,
				moneyTotal: 0,
				total: 0,

			}
		},
		onLoad(options) {
			// console.log("options.data", options.data)
			// let data = JSON.parse(decodeURIComponent(options.data))

		},
		onShow() {
			this.goodList = this.getCart()
			// this.typeId = data.goodsData[0].typeId
			this.calcTotal()
		},
		methods: {
			getCart() {
				const value = uni.getStorageSync('tjData');
				let json = []
				if (value) {
					json = JSON.parse(value)
				}
				return json
			},
			//计算总价
			calcTotal() {
				let list = this.goodList
				if (list.length === 0) {
					this.empty = true
					return
				}
				let total = 0
				let moneyTotal = 0
				list.forEach(item => {
					if (item.active) {
						total += (item.price * item.num)
						moneyTotal += (item.money * item.num)
					}
				})
				this.total = Number(total.toFixed(2))
				this.moneyTotal = Number(moneyTotal.toFixed(2))
			},
			dianJiQuanXuan() {
				let that = this
				this.allActive = !this.allActive
				let list = this.goodList
				list.forEach(item => {
					item.active = that.allActive
				})
				this.calcTotal()
			},
			dianjiActive(index) {
				this.goodList[index].active = !this.goodList[index].active
				this.changeAllActive()
			},
			changeAllActive() {
				let list = this.goodList
				if (list.length === 0) {
					this.allActive = false
					return
				}
				if (this.$_.every(list, 'active'))
					this.allActive = true
				else
					this.allActive = false
				this.calcTotal()
			},
			deleteCart(index) {
				this.goodList.splice(index, 1)
				uni.setStorage({
					key: 'tjData',
					data: JSON.stringify(this.goodList),
					success: function() {}
				});
			},
			async buttonClick() {
				let that = this
				let goodsData = []
				let flag = false
				this.goodList.forEach(item => {
					if (item.num > 0 && item.active) {
						flag = true
						goodsData.push(item)
					}
				})
				if (!flag) {
					this.$api.msg("最少选择一个商品")
					return false
				}
				this.$public.navto(`/pages/order/createOrder`, {
					data: JSON.stringify({
						goodsData: goodsData
					})
				})
			},
		},
	}
</script>

<style lang="scss" scoped>
	.content {
		padding: 30rpx;



		.delete-div {
			width: 90rpx;
			// height: 220rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			background: rgba(201, 2, 5, 0.1);
			border-radius: 0rpx 16rpx 16rpx 0rpx;
		}

		.delete {
			width: 48rpx;
			height: 48rpx;
		}

		.cart-list {
			display: flex;
			justify-content: space-between;
			background: #fff;
			padding: 30rpx;
			border-radius: 16rpx;
			flex-direction: row;
			align-items: stretch;
			margin-bottom: 30rpx;

			.left {
				width: 70rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				// height: 100%;

				.gou {
					width: 40rpx;
					height: 40rpx;
				}
			}

			.right {

				width: 560rpx;
				display: flex;

				.img {
					width: 176rpx;

					.image {
						width: 160rpx;
						height: 160rpx;
						border-radius: 10rpx;
					}
				}

				.text-div {
					width: 384rpx;
					// display: flex;

					.title {
						font-size: 28rpx;
						color: #333333;
						line-height: 40rpx;
						height: 80rpx;
						display: -webkit-box;
						/* 将文本显示为块级盒子 */
						-webkit-line-clamp: 2;
						/* 最多显示两行文本 */
						-webkit-box-orient: vertical;
						/* 设置盒子内文本的垂直方向排列 */
						overflow: hidden;
						/* 隐藏溢出的文本 */
						text-overflow: ellipsis;
						/* 显示省略号 */
						margin-bottom: 32rpx;
					}

					.text-bottom {
						height: 48rpx;
						display: flex;
						justify-content: space-between;

						.money {
							font-size: 32rpx;
							color: #DF686A;

							text {
								font-size: 24rpx;
							}
						}

						.num {
							.numcss {
								/deep/.uni-numbox-btns {
									border-radius: 50%;
								}

								/deep/.uni-numbox__value {
									background: #fff !important;
									color: #333 !important;
								}
							}
						}
					}
				}
			}
		}

		.page-bottom {
			position: fixed;
			left: 0;
			bottom: 0;
			z-index: 95;
			display: flex;
			justify-content: space-around;
			align-items: center;
			width: 100%;
			height: 140rpx;
			background: #f7f7f7;

			.page-bottom-l {
				display: flex;
				align-items: center;
				color: #333333;
				font-size: 28rpx;

				.gou {
					width: 40rpx;
					height: 40rpx;
					margin-right: 16rpx;
				}

				.zong-ji {
					font-size: 24rpx;
					color: #999999;
					padding-left: 24rpx;
				}

				.zong-money {
					color: #DF686A;
				}

			}

			// box-shadow: 0 0 20upx 0 rgba(0, 0, 0, .5);
			// border-radius: 16upx;
			uni-button {

				&.p-b-btn {
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					font-size: $font-sm;
					color: $font-color-base;
					width: 96upx;
					height: 100rpx;
					margin: 0;
					padding: 0;
					line-height: 1.8;
					background: none;
				}

				&:after {
					display: none;
				}
			}

			.p-b-btn {
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				font-size: $font-sm;
				color: $font-color-base;
				width: 96upx;
				height: 100rpx;
				margin: 0;
				padding: 0;
				line-height: 1.6;
				background: none;

				&:after {
					display: none;
				}

				.yticon {
					font-size: 40upx;
					line-height: 48upx;
					color: $font-color-light;

					&.yituuu {
						margin-top: 10upx;
						line-height: 36upx;
					}
				}

				&.active,
				&.active .yticon {
					color: $uni-color-primary;
				}

				.icon-fenxiang2 {
					font-size: 42upx;
					transform: translateY(-2upx);
				}

				.icon-shoucang {
					font-size: 46upx;
				}
			}

			.action-btn-group {
				display: flex;
				width: 320rpx;
				height: 88rpx;
				border-radius: 10rpx;
				overflow: hidden;
				margin-left: 20upx;

				// position: relative;
				.btncss {
					height: 88rpx;

				}
			}
		}
	}
</style>